
const puppeteer = require('puppeteer');
const fs = require('fs');
var http = require('http');
async function getWelfareImage(url) {

    // 返回解析为Promise的浏览器
   const browser = await puppeteer.launch();

    // 返回新的页面对象
   const page = await browser.newPage({headless: false});

    // 页面对象访问对应的url地址
   await page.goto(url, {
       waitUntil:'networkidle'
   })

    // 等待10s，等待浏览器的加载
   await page.waitFor(15000);

    // 可以在page.evaluate的回调函数中访问浏览器对象，可以进行DOM操作
   const urls = await page.evaluate(() => {
       var ol = document.getElementsByClassName('pic-box')
       var len = document.getElementsByClassName('pic-box').length
       var url = []
       for (let j = 0; j < len; j++) {
           var imgs = ol[j].getElementsByTagName('img')
           var content = ol[j].getElementsByTagName('p')
           for (let i = 0; i < imgs.length; i++) {
               url.push({ url:imgs[i].getAttribute('src'), title:content[0].innerText+'.'+imgs[i].getAttribute('src').split('.')[3] })
           }
       }
        // 返回所有url地址数组
        return url
   })
  // console.log(urls)

    for (var k = 0;k < urls.length; k++) {
        var imgurl = urls[k].url;
        var filename = urls[k].title;
        savedContent(imgurl, filename);
    }

    // 在本地存储所爬取内容
    function savedContent(imgurl, filename) {
      var streaml = fs.createWriteStream('./duowanimg/' + filename);
      http.get(imgurl, function (res) {
        res.pipe(streaml);
        console.log(filename);
      })
    }

    // 关闭无头浏览器
   await browser.close();
}
getWelfareImage('http://tu.duowan.com/scroll/138931.html');